package com.iot.service;

import com.iot.mapper.DTpostMapper;
import com.iot.mapper.TpostMapper;
import com.iot.pojo.MTpost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TpostServiceImpl implements TpostService {
    @Autowired
    private TpostMapper mapper;
   @Autowired
    private DTpostMapper dmapper;
    @Override
    public int getRecordCount(String sWhere) {
        return mapper.getRecordCount(sWhere);
    }
    @Override
    public int insert(MTpost mTpost) throws Exception {
        int count = 0;
        count = getRecordCount("AND APP_ID ="+ mTpost.getAPP_ID()+"  AND SNAME='" + mTpost.getSNAME() + "'");
        if (count > 0)
            throw new Exception("该名称已存在");
        return mapper.insert(mTpost);
    }

    @Override
    public void update(MTpost mTpost) throws Exception {
        int count = 0;
        count = getRecordCount("AND APP_ID ="+ mTpost.getAPP_ID()+"  AND SNAME='" + mTpost.getSNAME() + "' AND POST_ID!="+mTpost.getPOST_ID()+"");
        if (count > 0)
            throw new Exception("该名称已存在");
        mapper.update(mTpost);
    }

    @Override
    public void deleteById(int id) {
        mapper.deleteById(id);
    }

    @Override
    public MTpost single(Integer id) {
        return mapper.single(id);
    }

    @Override
    public List<MTpost> fetch(String sWhere) {
        return mapper.fetch(sWhere);
    }

    @Override
    public List<Map<String, Object>> fetchByPage(Map<String, Object> map) {

        return dmapper.fetchByPage(map);
    }

}
